//
//  ForgetPsdViewController.m
//  NoNameAmazing
//
//  Created by Hero on 17/9/1.
//  Copyright © 2017年 北京执行力科技有限公司. All rights reserved.
//

#import "ForgetPsdViewController.h"
#import "NewPassController.h"
@interface ForgetPsdViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *nameTf;
@property (weak, nonatomic) IBOutlet UITextField *mailBoxTf;
@property (weak, nonatomic) IBOutlet UITextField *yanZTf;

@end

@implementation ForgetPsdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"忘记密码";
    self.countTimeBtn.layer.cornerRadius = 4;
    self.countTimeBtn.layer.masksToBounds = YES;
    self.nextBtn.layer.cornerRadius = 4;
    self.countTimeBtn.layer.masksToBounds = YES;
    
    self.nameTf.delegate = self;
    self.mailBoxTf.delegate = self;
    self.yanZTf.delegate = self;
    
}
- (IBAction)nextBtnClick:(id)sender {
    if(_yanZTf.text.length>0&& _nameTf.text.length>0){
        NewPassController *newController = [[NewPassController alloc] init];
        newController.codeStr = _yanZTf.text;
        newController.name = _nameTf.text;
        [self.navigationController pushViewController:newController animated:YES];
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"用户名和验证码不能为空"
                                                                       message:nil
                                                                preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* action = [UIAlertAction actionWithTitle:@"确定"
                                                         style:UIAlertActionStyleDefault
                                                       handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }

    
}
- (IBAction)countTimeBtnClick:(id)sender {
    if(_mailBoxTf.text.length>0&&_nameTf.text.length>0){
        [self requestNum];
        
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"账号和邮箱不能为空"
                                                                       message:nil
                                                                preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* action = [UIAlertAction actionWithTitle:@"确定"
                                                         style:UIAlertActionStyleDefault
                                                       handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }

    
}

-(void)requestNum
{
    NSDictionary *params = @{@"userid":self.nameTf.text,
                             @"email":self.mailBoxTf.text
                             };
    [NetWorkManager requestWithType:1 withUrlString:@"/password/sendemail.do" withParaments:params withSuccessBlock:^(id object) {
        
        if ([object[@"status"]intValue]==1 ) {
           [self timeOut];
        }else{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:object[@"message"]
                                                                           message:nil
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction* action = [UIAlertAction actionWithTitle:@"确定"
                                                             style:UIAlertActionStyleDefault
                                                           handler:nil];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
        }
        
    } withFailureBlock:^(NSError *error) {
        
        
    } progress:^(float progress) {
        
    }];

}

-(void)timeOut
{
    __block int timeout=60; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(timeout<=0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                
                [_countTimeBtn setTitle:@"获取" forState:UIControlStateNormal];
                _countTimeBtn.userInteractionEnabled = YES;
                
            });
            
        }else{
            
            //            int minutes = timeout / 60;
            
            int seconds = timeout;
            
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                
                NSLog(@"____%@",strTime);
                
                [_countTimeBtn setTitle:[NSString stringWithFormat:@"%@",strTime] forState:UIControlStateNormal];
                
                
                _countTimeBtn.userInteractionEnabled = NO;
                
            });
            
            timeout--;
            
        }
        
    });
    
    dispatch_resume(_timer);
}

#pragma mark UITextFiledDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

@end
